home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 January / enter-2004-01.iso / files / maxima-5.9.0.exe / {app} / libexec / maxima / 5.9.0 / maxima-run-lisp next >
Encoding:
Text File  |  2003-02-09  |  11.7 KB  |  392 lines

  1. #! /bin/sh
  2. # -----------------------------------------------------------------------------
  3. #     Title: A script to invoke the various CL implementations in a uniform way
  4. #   Created: 1999-01-16 16:41
  5. #    Author: Gilbert Baumann <unk6@rz.uni-karlsruhe.de>
  6. #   License: LGPL (See file GNU-LGPL for details)
  7. # -----------------------------------------------------------------------------
  8. #  (c) copyright 1999 by Gilbert Baumann, Sam Steingold, Bruno Haible
  9. # $Id: maxima-run-lisp,v 1.4 2002/10/17 21:08:38 amundson Exp $
  10. # $Source: /cvsroot/maxima/maxima/lisp-utils/maxima-run-lisp,v $
  11.  
  12. # Prior to invocation the LISPTYPE environment variable must been set to
  13. # one of these:
  14. #
  15. #    clisp      uses ${CLISP} if set, else "clisp"
  16. #    cmucl      uses ${CMUCL} if set, else "lisp"
  17. #    acl43      uses ${ACL43} if set, else "cl"
  18. #    acl5       uses ${ACL5} if set, else "cl"
  19. #    gcl        uses ${GCL} if set, else "gcl"
  20. #    sbcl       uses ${SBCL} if set, else "sbcl"
  21.  
  22. usage(){
  23.     cat <<\EOF
  24. Usage:
  25.  run-lisp [clause ...] [argument ...]
  26.      clause ::= -i file    ; load file `file'
  27.               | -c file    ; compile file `file'
  28.               | -x form    ; execute form `form'
  29.               | -I image   ; use image file `image'
  30.               | -d image   ; dump to image `image'
  31.           | -f initfun ; set init function of dumped image to `initfun'
  32.           | --extra-args=args ; add 'args' to lisp command line
  33.               | --safety n ; set safety level for compilation
  34.               | --speed n  ; set speed level for compilation
  35.  
  36.      Note: Lisp specific extensions (e.g. .dxl on ACL or .mem for CLISP)
  37.            are not to be included into the image argument
  38.  
  39.      Anything else is stuffed into the Lisp variable 'argv', which is
  40.      available in the lexical environment forms given to -x are
  41.      evaluated in.
  42.  
  43.  run-lisp [--extra-args=args] -run image [-i file]
  44.    interactively run 'image'
  45.    (user:run) will be called upon start up.
  46.  
  47. Makefile support
  48.  
  49.  run-lisp -faslext
  50.    echo the fasload file extension to stdout.
  51.    This is useful for Makefile; you could then say e.g.
  52.    FAS:=$(shell $(TOP)/bin/run-lisp -faslext)
  53.  
  54.  run-lisp -dumpext
  55.    echo the memory image extension _with_ dot.
  56.    usage: (like above)
  57.    DUMP:=$(shell $(TOP)/bin/run-lisp -dumpext)
  58.  
  59.  run-lisp -cat [fasl-file ...]
  60.    Cat all the given fasl files together.
  61.  
  62. EXAMPLE
  63.  
  64.    $ run-lisp -x "(print argv)" foo bar baz "Hallo Welt" foo\"bar
  65.    ("foo" "bar" "baz" "Hallo Welt" "foo\"bar")
  66.    $
  67. EOF
  68.     exit 0;
  69. }
  70.  
  71. fail(){
  72.     echo "$0: $*" 1>&2
  73.     exit 1
  74. }
  75.  
  76. case "$1" in
  77.   --extra-args=*)
  78.             extra_args=`echo $1 | sed 's/--extra-args=//'`
  79.             shift 1
  80.             ;;
  81.   *)
  82.    ;;
  83. esac
  84.  
  85. case "$1" in
  86.   "--help" | "-h" | "-?" )
  87.       usage
  88.       ;;
  89.   "-cat" )
  90.       shift
  91.       case "$LISPTYPE" in
  92.         clisp | cmucl | acl43 | acl5 )
  93.             cat "$@"
  94.             ;;
  95.         * )
  96.             fail "Sorry, option -cat not supported for LISPTYPE=${LISPTYPE}."
  97.             ;;
  98.       esac
  99.       ;;
  100.   "-faslext" )
  101.       shift
  102.       case "$LISPTYPE" in
  103.         clisp )
  104.             # This is (pathname-type (car (system::*compiled-file-types*))).
  105.             echo 'fas'
  106.             ;;
  107.         cmucl )
  108.             # This can be found via
  109.             #    (c:backend-fasl-file-type c:*target-backend*),
  110.             # but for speed we look at the uname first.
  111.             case `uname -m 2>/dev/null` in
  112.                 i[3-6]86 )
  113.                    echo 'x86f'
  114.                    ;;
  115.                 * )
  116.                   # Call .
  117.                   ${CMUCL-lisp} -noinit \
  118.                          -eval "(progn \
  119.                                   (write-string (c:backend-fasl-file-type c:*target-backend*))
  120.                                   (terpri)
  121.                                   (ext:quit 0))"
  122.                   ;;
  123.             esac
  124.             ;;
  125.         acl43 | acl5 )
  126.             echo 'fasl'
  127.             ;;
  128.         gcl )
  129.             echo 'o' # but also 'data' on same platforms
  130.             ;;
  131.         sbcl )
  132.             echo 'x86f' # How about other platforms?
  133.             ;;
  134.         * )
  135.             # Since make does not stop when the exit status is 1, we simply
  136.             # echo LISPTYPE_NOT_SET here.
  137.             echo LISPTYPE_NOT_SET
  138.             fail "Sorry, option -faslext not supported for LISPTYPE=${LISPTYPE}."
  139.             ;;
  140.       esac
  141.       ;;
  142.   "-dumpext" )
  143.       shift 1
  144.       case "$LISPTYPE" in
  145.           clisp)
  146.               echo .mem
  147.           ;;
  148.           acl43)
  149.               echo ""
  150.           ;;
  151.           acl5)
  152.               echo .dxl
  153.           ;;
  154.           cmucl)
  155.               echo .core
  156.           ;;
  157.           sbcl)
  158.               echo .core # This is just a guess
  159.           ;;
  160.           *)
  161.               # Since make does not stop when the exit status is 1, we simply
  162.               # echo LISPTYPE_NOT_SET here.
  163.               echo LISPTYPE_NOT_SET
  164.               fail "Sorry, option -dumpext not supported for LISPTYPE=${LISPTYPE}."
  165.           ;;
  166.       esac
  167.     ;;
  168.   "-run" )
  169.     # we special case on '-run' for now
  170.     shift 1
  171.     if [ x"$2" = x"-i" ]; then
  172.       load_file=$3
  173.     fi
  174.     case "$LISPTYPE" in
  175.         clisp)
  176.         if [ -n "$load_file" ]; then
  177.           load_arg="-i $load_file"
  178.         fi
  179.             ${CLISP-clisp} $extra_args $load_arg -M "$1".mem 
  180.         ;;
  181.         cmucl )
  182.         if [ -n "$load_file" ]; then
  183.           load_arg="-load $load_file"
  184.         fi
  185.         # jfa: fixme 04/24/02
  186.         # jfa: The -eval part of the following line is a big hack!
  187.             ${CMUCL-lisp} -eval "(user::run)" $extra_args $load_arg -core "$1".core
  188.             ;;
  189.         acl43 )
  190.         if [ -n "$load_file" ]; then
  191.           load_arg="-i $load_file"
  192.         fi
  193.             # Why "$1"? ACL4.3 dumps executables
  194.             "$1" $extra_args $load_arg -e '(unwind-protect (run) (excl:exit))'
  195.             ;;
  196.         acl5 )
  197.         if [ -n "$load_file" ]; then
  198.           load_arg="-i $load_file"
  199.         fi
  200.             ${ACL5-cl} $extra_args $load_arg -I "$1".dxl -e '(unwind-protect (run) (excl:exit))'
  201.             ;;
  202.         gcl )
  203.         if [ -n "$load_file" ]; then
  204.           load_arg="-load $load_file"
  205.         fi
  206.             "$1" $extra_args $load_arg -eval '(run)'
  207.             ;;
  208.         *)
  209.             fail "Sorry, option -run not supported for LISPTYPE=${LISPTYPE}."
  210.         ;;
  211.     esac
  212.     ;;
  213.   * )
  214.     # Multiple arguments.
  215.     unset image
  216.     todo=""             # list of forms to execute
  217.     args=""             # list of arguments (strings) to pass
  218.     while [ $# != 0 ]; do
  219.       case "$1" in
  220.         -i)
  221.             if [ $# = 1 ]; then
  222.               fail "missing argument for $1"
  223.             fi
  224.             shift
  225.             backslashify='s,\(["\\]\),\\\1,g'
  226.             arg=`echo "$1" | sed -e "$backslashify"`
  227.             todo=$todo" (load \"${arg}\")"
  228.             shift
  229.             ;;
  230.         -x)
  231.             if [ $# = 1 ]; then
  232.               fail "missing argument for $1"
  233.             fi
  234.             shift
  235.             todo=$todo" $1"
  236.             shift
  237.             ;;
  238.         -c)
  239.             if [ $# = 1 ]; then
  240.               fail "missing argument for $1"
  241.             fi
  242.             shift
  243.             backslashify='s,\(["\\]\),\\\1,g'
  244.             arg=`echo "$1" | sed -e "$backslashify"`
  245.             # todo=$todo" (compile-file \"${arg}\" :print nil)"
  246.             # truename helps, when using Franz' emacs interface
  247.             todo=$todo" (compile-file (truename \"${arg}\") :print nil)"
  248.             shift
  249.             ;;
  250.         -I)
  251.             if [ $# = 1 ]; then
  252.               fail "missing argument for $1"
  253.             fi
  254.             shift
  255.             image="$1"
  256.             shift
  257.             ;;
  258.         -f)
  259.             if [ $# = 1 ]; then
  260.               fail "missing argument for $1"
  261.             fi
  262.             shift
  263.             initfun=$1
  264.             case "$LISPTYPE" in
  265.               clisp )
  266.                   initfun_arg=":init-function #'$initfun"
  267.                   ;;
  268.               cmucl )
  269.                   initfun_arg=":init-function #'$initfun"
  270.                   ;;
  271.               sbcl )
  272.                   initfun_arg=":toplevel #'$initfun"
  273.                   ;;
  274.               gcl )
  275.                   todo=$todo" (setq si::*top-level-hook* #'$initfun)"
  276.                   ;;
  277.               * )
  278.                   fail "Sorry, option -d not supported for LISPTYPE=${LISPTYPE}."
  279.                   ;;
  280.             esac
  281.             shift
  282.             ;;
  283.         -d)
  284.             if [ $# = 1 ]; then
  285.               fail "missing argument for $1"
  286.             fi
  287.             shift
  288.             backslashify='s,\(["\\]\),\\\1,g'
  289.             arg=`echo "$1" | sed -e "$backslashify"`
  290.             case "$LISPTYPE" in
  291.               clisp )
  292.                   todo=$todo" (#+lisp=cl ext:saveinitmem #-lisp=cl lisp:saveinitmem \"${arg}.mem\" $initfun_arg)"
  293.                   ;;
  294.               cmucl )
  295.                   todo=$todo" (ext:save-lisp \"${arg}.core\" $initfun_arg)"
  296.                   ;;
  297.               sbcl )
  298.                   todo=$todo" (sb-ext:save-lisp-and-die \"${arg}.core\" $initfun_arg)"
  299.                   ;;
  300.               acl43 )
  301.                   todo=$todo" (excl:dumplisp :name \"${arg}\")"
  302.                   ;;
  303.               acl5 )
  304.                   todo=$todo" (excl:dumplisp :name \"${arg}.dxl\")"
  305.                   ;;
  306.               gcl )
  307.                   todo=$todo" (si:save-system \"${arg}\")"
  308.                   ;;
  309.               * )
  310.                   fail "Sorry, option -d not supported for LISPTYPE=${LISPTYPE}."
  311.                   ;;
  312.             esac
  313.             shift
  314.             ;;
  315.         --safety)
  316.             if [ $# = 1 ]; then
  317.               fail "missing argument for $1"
  318.             fi
  319.             shift 1
  320.             todo=$todo" (proclaim (quote (optimize (safety "$1"))))"
  321.             shift 1
  322.         ;;
  323.         --speed)
  324.             if [ $# = 1 ]; then
  325.               fail "missing argument for $1"
  326.             fi
  327.             shift 1
  328.             todo=$todo" (proclaim (quote (optimize (speed "$1"))))"
  329.             shift 1
  330.         ;;
  331.         *)
  332.             backslashify='s,\(["\\]\),\\\1,g'
  333.             arg=`echo "$1" | sed -e "$backslashify"`
  334.             args=$args" \"${arg}\""
  335.             shift
  336.             ;;
  337.       esac
  338.     done
  339.  
  340.     # done with collecting the arguments
  341.  
  342.     if [ x"$LISPTYPE" = x"sbcl" ]; then
  343.       todo="(progn${todo}(sb-ext:quit))"
  344.     else
  345.       todo="(progn${todo})"
  346.     fi
  347.     args="(${args})"
  348.     todo="(let ((argv '$args)) (declare (ignorable argv)) $todo (values))"
  349.  
  350.     case "$LISPTYPE" in
  351.       clisp )
  352.           todo="(progn (setq #+lisp=cl ext:*load-paths* #-lisp=cl lisp:*load-paths* '(#P\"\")) ${todo})"
  353.           test -z "$image" || image="-M ${image}.mem";
  354.           echo ${CLISP-clisp} -norc -q ${image} -x "$todo"
  355.           exec ${CLISP-clisp} $extra_args -norc -q ${image} -x "$todo"
  356.           ;;
  357.       cmucl )
  358.           # we have to convince CMUCL to return a proper exit status.
  359.           todo="(let (.res (.cond t))
  360.                   (unwind-protect
  361.                     (multiple-value-setq (.res .cond)
  362.                        (ignore-errors (progn $todo
  363.                                         (fresh-line) (finish-output))))
  364.                     (unix:unix-exit (if .cond 1 0))))"
  365.           test -z "$image" || image="-core ${image}.core";
  366.       exec ${CMUCL-lisp} $extra_args -noinit ${image} -eval "$todo"
  367.           ;;
  368.       acl43 )
  369.           exec echo "$todo" | ${image-${ACL43-cl}} $extra_args -batch
  370.           ;;
  371.       acl5 )
  372.           test -z "$image" || image="-I ${image}.dxl";
  373.           exec echo "$todo" | ${ACL5-cl} ${image+"-I ${image}.dxl"} $extra_args -batch
  374.           ;;
  375.       gcl )
  376.         echo ${image-${GCL-gcl}} $extra_args -batch -eval "$todo"
  377.           ${image-${GCL-gcl}} $extra_args -batch -eval "$todo"
  378.       ;;
  379.       sbcl )
  380.           ${image-${SBCL-sbcl}} $extra_args --noinform --noprint --eval "$todo"
  381.           ;;
  382.       * )
  383.           if [ -n "$LISPTYPE" ] ; then
  384.             fail "Sorry, LISPTYPE=${LISPTYPE} is not supported"
  385.           else
  386.             fail "LISPTYPE environment variable is not set"
  387.           fi
  388.           ;;
  389.     esac
  390.  
  391. esac
  392.